//
//  CTVChannelInfoOverlayController.m
//  Channltv
//
//  Created by Bruno Keymolen on 27/03/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "CTVChannelInfoOverlayController.h"


@implementation CTVChannelInfoOverlayController


- (id) initWithScene: (BRRenderScene *)scene andChannel:(CTVChannel*)channel andTimeOut:(double)seconds{

	[super initWithScene:scene];

	
	m_channel = channel;
	[m_channel retain];

	
	//Build GUI elements
	//Title
	m_titlecontrol = [[BRTextControl alloc] initWithScene: scene];
	[m_titlecontrol setText:[m_channel name]];
	[m_titlecontrol setTextAttributes:[[BRThemeInfo sharedTheme] mediumTextAttributes]];
	[self addControl:m_titlecontrol];

	m_subTitlecontrol = [[BRTextControl alloc] initWithScene: scene];
	[m_subTitlecontrol setText:[m_channel name]];
	[m_subTitlecontrol setTextAttributes:[[BRThemeInfo sharedTheme] smallTextAttributes]];
	[self addControl:m_subTitlecontrol];


	//channel img
	m_thisChannelImg = [[BRImageControl alloc] initWithScene: [self scene]];
	[m_thisChannelImg setReflectionAmount:0.0f];
	[m_thisChannelImg setReflectionOffset:0.0f];
	[self addControl:m_thisChannelImg];
	

	[self _layout];

	 	
	//Destroy ourselves within  m_selectTime seconds
	m_timer = [NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(_timerHandler:) userInfo:nil repeats:NO];
	[m_timer retain];
	
	return self;
}

- (void) dealloc{
	[m_titlecontrol release];
	[m_subTitlecontrol release];
	[m_channel release];
	[m_timer invalidate];
	[m_timer release];

	[m_thisChannelImg release];
	
	[super dealloc];
}

- (void) setFrame: (NSRect) frame{
    [[super masterLayer] setFrame: frame];

	[self _layout];
}

- (BOOL)brEventAction:(BREvent*)event{
	BOOL ret;

	//We are only interested in the key-down events.
	if([event value] == 0){
		return [super brEventAction:event];
	}
	switch([event remoteAction]){
		case kBRRemoteActionMenu:
			ret = [super brEventAction:event];
			break;
		case kBRRemoteActionUp:
		case kBRRemoteActionDown:
			if(m_timer)[m_timer fire];
			ret = TRUE;
			break;
	}
	
	return ret;
}



//Private
-(void)_layout{
	NSRect masterFrame = [[self masterLayer] frame];
	NSRect titleframe = masterFrame;    	
	
	int offsetX = 70;
	
	//img - left upper corner
	[m_thisChannelImg setTexture:[m_channel thumbTextureForScene:[self scene]]];
	NSRect img1Rect;
	img1Rect.size.height = masterFrame.size.height * 0.15;
	img1Rect.size.width = img1Rect.size.height;
	img1Rect.origin.x =  offsetX;
	img1Rect.origin.y =  masterFrame.size.height - img1Rect.size.height - 20;
	[m_thisChannelImg setFrame:img1Rect];

	NSRect subtitleframe = masterFrame;    	
	NSString* clipTitle = [[m_channel currentClipToPlay] getTitle];
	[m_subTitlecontrol setText:clipTitle];
	NSSize subTitleTextSize = [m_subTitlecontrol renderedSize];
	subtitleframe.size.height = subTitleTextSize.height;
	subtitleframe.origin.y =  masterFrame.size.height - img1Rect.size.height - 20;
	subtitleframe.origin.x = img1Rect.origin.x + img1Rect.size.height + 10;
	[m_subTitlecontrol setFrame:subtitleframe];
	
	[m_titlecontrol setText:[m_channel name]];
	NSSize titleTextSize = [m_titlecontrol renderedSize];
	titleframe.size.height = titleTextSize.height;
	titleframe.origin.y = subtitleframe.origin.y + titleTextSize.height + 1;
	titleframe.origin.x = img1Rect.origin.x + img1Rect.size.height + 10;
	[m_titlecontrol setFrame:titleframe];
	
	
	
	
	
}


-(void)_timerHandler:(NSTimer*)timer{
	[[self stack] removeController:self];	
}





@end
